package com.algorithm.cjm.july.chapter1.ch1;

/**
 * 单词翻转
 * 输入一个英文句子，翻转句子中单词的顺序。要求单词内字符的顺序不变，句子中单词以空格符隔开。
 * eg: I am a student.  则输出 "student. a am I"
 *
 * Created by jieming.chen on 2020/1/11
 */
public class WordReversal {
    private static WordReversal wordReversal = new WordReversal();

    public static void main(String[] args) {
        String target = "I am a student.";
        String res = "student. a am I";
        System.out.println(wordReversal.wordReversal(target));
    }

    public String wordReversal(String target) {
        String[] splits = target.split(" ");
        flip(splits);
        return String.join(" ", splits);
    }

    public void flip(String[] chars) {
        int i = 0;
        int j = chars.length - 1;
        while (i < j) {
            String temp = chars[i];
            chars[i] = chars[j];
            chars[j] = temp;
            i++;
            j--;
        }
    }
}
